#include "Texture.h"

namespace Render
{
	bool Texture::setData(const Resource::Image *image)
	{
		ASSERT(image);
		
		m_width  = image->width;
		m_height = image->height;
		
		GLint  iformat;
		GLenum format;
		
		switch (image->bpp)
		{
			case 8:
				iformat = GL_R8;
				format  = GL_RED;
				break;
			case 24:
				iformat = GL_RGB8;
				format  = GL_RGB;
				break;
			case 32:
				iformat = GL_RGBA8;
				format  = GL_RGBA;
				break;
			default:
				LOG_ERROR("Unknown image bpp %u\n", image->bpp);
				return false;
		}
		
		glBindTexture(GL_TEXTURE_2D, m_handle);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, iformat, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, GL_PVOID(image->data));
		glGenerateMipmap(GL_TEXTURE_2D);
		
		return true;
	}
	
	bool Texture::setData(const Resource::FontDesc *font)
	{
		ASSERT(font);
		
		glBindTexture(GL_TEXTURE_2D, m_handle);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8_ALPHA8, font->width, font->height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, GL_PVOID(font->bitmap));
		
		return true;
	}
}
